package com.wenhao.xwbank;

import java.util.Scanner;

/**
 * @author Wenhao Tong
 * @create 2021-08-28 20:23
 */
class Solution {
    /* Write Code Here */
    public int[] circlePrint(int[][] input) {
        int top = 0;
        int bottom = input.length - 1;
        int left = 0;
        int right = input[0].length - 1;
        int[] res = new int[input.length * input[0].length];
        int index = 0;
        while (top <= bottom && left <= right) {
            for (int i = left;i <= right;i++) {
                res[index++] = input[top][i];
            }
            top++;
            for (int i = top;i <= bottom;i++) {
                res[index++] = input[i][right];
            }
            right--;
            if (top > right) {
                break;
            }
            for (int i = right;i >= left;i--) {
                res[index++] = input[bottom][i];
            }
            bottom--;
            for (int i = bottom;i >= top;i--) {
                res[index++] = input[i][left];
            }
            left++;
        }
        return res;
    }
}

public class Main {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int[] res;

        int _input_rows = 0;
        int _input_cols = 0;
        _input_rows = Integer.parseInt(in.nextLine().trim());
        _input_cols = Integer.parseInt(in.nextLine().trim());

        int[][] _input = new int[_input_rows][_input_cols];
        for(int _input_i=0; _input_i<_input_rows; _input_i++) {
            for(int _input_j=0; _input_j<_input_cols; _input_j++) {
                _input[_input_i][_input_j] = in.nextInt();

            }
        }

        if(in.hasNextLine()) {
            in.nextLine();
        }

        res = new Solution().circlePrint(_input);
        for(int res_i=0; res_i < res.length; res_i++) {
            System.out.println(String.valueOf(res[res_i]));
        }

    }
}
